import {
    _decorator,
    Color,
    color,
    Component,
    instantiate,
    Label,
    Node,
    size,
    tween,
    UITransform,
    v2,
    v3,
    Vec2,
    Vec3
} from 'cc';
import {Grid} from "./Grid";


import {GameMain} from "./GameMain";
import {AudioClipID, AudioManager} from "./AudioManager";

const { ccclass, property } = _decorator;

@ccclass('Guide')
export class Guide extends Component
{

    private maskNode:Node=null;
    private maskUITransform:UITransform=null;
    private fingerNode:Node=null;

    private tipLabel1:Label=null;
    private tipLabel2:Label=null;
    private tipLabel3:Label=null;
    private tipLabel4:Label=null;

    public step:number=0;

    public bricks:Array<Node>=[];
    public fingerMoveIdx:number=-1;

    onLoad()
    {
        this.maskNode=this.node.getChildByName("guideMask").getChildByName("Mask");
        this.maskUITransform=this.maskNode.getComponent(UITransform);
        this.fingerNode=this.node.getChildByName("finger");

        this.tipLabel1=this.node.getChildByName("tipLabel1").getComponent(Label);
        this.tipLabel2=this.node.getChildByName("tipLabel2").getComponent(Label);
        this.tipLabel3=this.node.getChildByName("tipLabel3").getComponent(Label);
        this.tipLabel4=this.node.getChildByName("tipLabel4").getComponent(Label);
    }

    start()
    {

    }

    public isInGuide():boolean
    {
        return this.step>0;
    }

    public startFingerMove()
    {
        if(this.bricks.length<2)
        {
            this.stopFingerMove();
            return;
        }
        this.fingerNode.active=true;
        this.fingerMoveIdx=1;
        if(this.bricks[0]===null)
        {
            this.stopFingerMove();
            return;
        }
        this.fingerNode.setPosition(this.bricks[0].getPosition());
        tween(this.fingerNode).to(0.6, {position:this.bricks[this.fingerMoveIdx].getPosition() }).call(()=>{
            this.fingerMoveNext();
        }).start();

    }

    public fingerMove()
    {
        if(this.bricks.length<2)
        {
            this.stopFingerMove();
            return;
        }
        tween(this.fingerNode).to(0.6, {position:this.bricks[this.fingerMoveIdx].getPosition() }).call(()=>{
            this.fingerMoveNext();
        }).start();
    }

    public fingerMoveNext()
    {
        if(this.bricks.length<2)
        {
            this.stopFingerMove();
            return;
        }
        this.fingerMoveIdx++;
        if(this.fingerMoveIdx>=this.bricks.length)
        {
            this.fingerMoveIdx=0;
            this.scheduleOnce(()=>{
                this.fingerNode.active=false;
            }, 0.6);
            this.scheduleOnce(()=>{
                this.startFingerMove();
            }, 2);
        }
        else
        {
            this.fingerMove();
        }
    }

    public stopFingerMove()
    {
        this.fingerNode.active=false;
        this.fingerMoveIdx=-1;
    }

    public notifyMergeBrick(brick:Brick)
    {
    }

    update(deltaTime: number)
    {
    }
}

